import allure
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait




class Base:
    # 初始化
    def __init__(self,driver):
        # options = webdriver.ChromeOptions()
        # options.add_experimental_option("excludeSwitches", ["enable-logging"])
        # self.driver = webdriver.Chrome(options=options)
        # self.driver.maximize_window()
        # self.driver.get("http://127.0.0.1/Home/user/login.html")
        self.driver = driver




    # 查找元素方法 (提供：点击、输入、获取文本)使用
    def base_find_element(self, loc, timeout=30, poll=0.5):
        return WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll).until(lambda x: x.find_element(*loc))

    # 点击方法
    def base_click(self,loc):
        self.base_find_element(loc).click()

    # 输入方法
    def base_input(self,loc,value):
        element = self.base_find_element(loc)
        # 清空
        element.clear()
        # 输入
        element.send_keys(value)

    # 输入方法_不用清除
    def base_input_no_clear(self,loc,value):
        element = self.base_find_element(loc)
        # 输入
        element.send_keys(value)

    # 获取文本方法
    def base_get_text(self,loc):
        element=self.base_find_element(loc)
        return element.text

    # 截图方法
    def base_get_image(self):
        self.driver.get_screenshot_as_file("fail.png")

    # allure报告截图方法
    def base_get_allure_image(self):
        allure.attach(self.driver.get_screenshot_as_png(), "截图", allure.attachment_type.PNG)

    #移动滚动条方法
    def base_move_scroll_bar(self,y):
        js = "window.scrollTo(0, {})".format(y)
        self.driver.execute_script(js)

    #鼠标双击方法
    def base_double_click(self,loc):
        ActionChains(self.driver).double_click(self.base_find_element(loc)).perform()


    #鼠标悬停方法
    def base_move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.base_find_element(loc)).perform()

    #鼠标右键方法
    def base_right_click(self,loc):
        ActionChains(self.driver).context_click(self.base_find_element(loc)).perform()

    #下拉框方法----根据选择option选项
    def base_select_by_option(self,loc,option):
        ele=self.base_find_element(loc)
        # 实例化select对象
        select = Select(ele)
        # 通过select对象的index来选择广州
        select.select_by_index(option)

    #检测iframe是否可用,可用就直接切入
    def base_if_frame_exist(self,loc):
        try:
            element = WebDriverWait(self.driver, timeout=5).until(
                expected_conditions.frame_to_be_available_and_switch_to_it(loc))
            return element
        except:
            return False

    #查找元素判断是否操作成功
    def base_if_element_exist(self,loc):
        try:
            self.base_find_element(loc, timeout=4)
            return True
        except:
            return False


    #后台登录封装
    def base_ht_login(self, username, password, code):
        # TODO 输入用户名
        login_username = By.NAME, 'username'
        self.base_input_no_clear(login_username,username)
        # TODO 输入密码
        login_pwd = By.NAME, 'password'
        self.base_input_no_clear(login_pwd,password)
        # TODO 输入验证码
        login_verify_code = By.NAME, 'vertify'
        self.base_input_no_clear(login_verify_code,code)
        # TODO 点击登录按钮
        login_btn = By.NAME, 'submit'
        self.base_click(login_btn)

    #前台登录封装
    def base_qt_login(self, username, password, code):
        # TODO 输入用户名
        login_username = By.ID, "username"
        self.base_input_no_clear(login_username,username)
        # TODO 输入密码
        login_pwd = By.ID, "password"
        self.base_input_no_clear(login_pwd,password)
        # TODO 输入验证码
        login_verify_code = By.ID, "verify_code"
        self.base_input_no_clear(login_verify_code,code)
        # TODO 点击登录按钮
        login_btn = By.CSS_SELECTOR, ".J-login-submit"
        self.base_click(login_btn)